/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/05/15 by OE $
*/
$(function(){
	//加载下一页聊天记录
	$(document).on("click", "[f='but_get_msgchatpage']", function(){
	//$("[f='but_get_msgchatpage']").on("click", function(){
		$_page = parseInt($(this).attr("data-page"));
		if ($_page > 1) {
			$("[f='but_get_msgchatpage']").find("label").show();
		}
		_getMsgchatPageList($_page);
	});

	//加载下一页联系人
	$(document).on("click", "[f='but_get_msgcontactpage']", function(){
	//$("[f='but_get_msgcontactpage']").on("click", function(){
		$_page = parseInt($(this).attr("data-page"));
		$_type = $(this).attr("data-usertype");
		if (typeof($_type) == "undefined") {
			$_type = 0;
		}
		if ($_page > 1) {
			$("[f='but_get_msgcontactpage']").find("label").show();
		}
		_getMsgcontactPageList($_page, $_type);
	});
	
	//切换左边选项卡 2017.03.15
	$(document).on("click", "[f='chat_sel_tab']", function(){
		$tab = $(this).attr("data-tab");
		$("[f='chat_sel_tab']").removeClass("current");
		$(this).addClass("current");

		if ($tab == "chating") { //当前聊天会员
			$("#chating_user_data").show();
			$("#chating_contact_list").hide();

			_getChatingUserData("userleft");
			_getChatingUserData("userright");
			_getMsgchatPageList(1);
		}
		else { //好友、联系人列表
			$("#chating_user_data").hide();
			$("#chating_contact_list").show();
			if ($tab == "friend") {
				$type = 1;
				$("[f='but_get_msgcontactpage']").attr("data-usertype", 1);
			}
			else {
				$type = 0;
				$("[f='but_get_msgcontactpage']").attr("data-usertype", 0);
			}
			_getMsgcontactPageList(1, $type); //加载我的联系人
		}
	});

	//点击联系人聊天 2017.03.15
	$(document).on("click", "[f='but_open_chatuser']", function(){
	//$("[f='but_open_chatuser']").on("click", function(){
		$chat_uid = $(this).attr("data-touid");
		$chat_sid = $(this).attr("data-sid");

		if ($chat_sid.length > 0) { //存在会话
			$("#msgchat_uid").val($chat_uid);
			$("#msgchat_sid").val($chat_sid);

			$("#chating_user_data").show();
			$("#chating_contact_list").hide();
			
			$("[f='chat_sel_tab']").removeClass("current");
			$("#tab_chating").addClass("current");

			//$("#msgchat_sendgift").attr("data-touid", $chat_uid);

			//加载聊天信息
			_getChatingUserData("userleft");
			_getChatingUserData("userright");
			_getMsgchatPageList(1);
		}
		else { //创建会话
			$.ajax({
				type: "POST",
				url: _ROOT_PATH+"index.php?m=user&c=msgchat",
				cache: false,
				data: {a:"setsid", touid:$chat_uid, r:get_rndnum(8)},
				dataType: "json",
				success: function($data) {
					$json = eval($data);
					$response = $json.response;
					$result = $json.result;
					if ($response == '1') {
						$("#msgchat_uid").val($chat_uid);
						$("#msgchat_sid").val($result);

						$("#chating_user_data").show();
						$("#chating_contact_list").hide();
						
						$("[f='chat_sel_tab']").removeClass("current");
						$("#tab_chating").addClass("current");

						//$("#msgchat_sendgift").attr("data-touid", $chat_uid);

						//加载聊天信息
						_getChatingUserData("userleft");
						_getChatingUserData("userright");
						_getMsgchatPageList(1);
					}
					else {
						if ($result.length > 0) {
							ToastShow($result);
						}
						else {
							ToastShow("加载聊天会员错误");
						}
					}
				},
				error: function() {
				}
			});
		}

	});

    //切换表情
	//$("[f='msgchat_tab_em']").on("click", function(){
	$(document).on("click", "[f='msgchat_tab_em']", function(){
		if ($("#msg_face_box").css("display") == "block") {
			$("#msg_face_box").hide();
		}
		else {
			$("#msg_face_box").show();
			new Swiper('.swiper-container', {
				pagination: '.swiper-pagination',
				paginationClickable: true
			});
		}
	});
	
	//选择表情
	//$("[f='but_msgchat_selectem']").on("click", function(){
	$(document).on("click", "[f='but_msgchat_selectem']", function(){
		$_id = $(this).attr("data-id");
		$tag_em = "{emf_"+$_id+"}";
		var myField = document.getElementById("msgchat_content");
		if (document.selection) {
			//myField.focus();
			sel = document.selection.createRange();
			sel.text = $tag_em;
			//myField.focus();
		}
		else if (myField.selectionStart || myField.selectionStart == '0') {
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = endPos;
			myField.value = myField.value.substring(0, startPos)
						  + $tag_em
						  + myField.value.substring(endPos, myField.value.length);
			cursorPos += $tag_em.length;
			//myField.focus();
			myField.selectionStart = cursorPos;
			myField.selectionEnd = cursorPos;
		}
		else {
			myField.value += $tag_em;
			//myField.focus();
		}
	});


	//发送消息
	//$("[f='but_sendmsg']").on("click", function(){
	$(document).on("click", "[f='but_sendmsg']", function(){
		$_content = $("#msgchat_content").val();
		$touid = $("#msgchat_uid").val();
		if ($_content.length == 0) {
			return false;
		}
		$(".msgchat_send_mask").show();
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?m=user&c=msgchat",
			cache: false,
			data: {a:"savewrite", touid:$touid, content:$_content, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;

				if ($response == '1') {
					$("#msgchat_content").val("");
					$new_html = "<div class='log_r'>"+
								"<span class='jiao'></span>"+$result+
								"</div>";
					$("#chat_log_data").append($new_html);
					$("#msg_face_box").hide();
					//_hashFooter();
					$(".msgchat_send_mask").hide();
				}
				else if ($response == '2') { //发送次数已满 7.05.15
					$(".msgchat_no_power").show();
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("发送失败");
					}
				}
			},
			error: function() {
			}
		});

	});
	//关闭发送次数已满 7.05.15
	$(document).on("click", ".close_msgchat_no_power", function(){
		$(".msgchat_no_power").hide();
		$(".msgchat_send_mask").hide();
	});
	//升级会员 7.05.15
	$(document).on("click", ".open_vip_msgchat", function(){
		window.location.href = _ROOT_PATH+"index.php?m=user&c=vip";
	});


		
	//关闭解锁方式
	$(document).on("click", "[f='but_close_free_msgchat']", function(){
	//$("[f='but_close_free_msgchat']").on("click", function(){
		$("#free_msgchat_data").fadeOut(JS_FADEOUT_TIME, function(){
			$("#free_msgchat_box").hide();
		});
	});

	//打开解锁方式
	$(document).on("click", "[f='but_open_free_msgchat']", function(){
	//$("[f='but_open_free_msgchat']").on("click", function(){
		$("#free_msgchat_box").show();
		$("#free_msgchat_data").fadeIn(JS_FADEIN_TIME);
	});

	
	//弹出付款方式
	$(document).on("click", "[f='but_open_msgchat_paytype']", function(){
		$_sid = $("#msgchat_sid").val();
		if (typeof($_sid) == "undefined") {
			$_sid = "";
		}
		$_url = _ROOT_PATH+"index.php?m=user&c=msgchat&a=pay&sid="+$_sid+"&r="+get_rndnum(8);
		oeShadeBox("pop_pay_msgchat", "ajax", $_url, 690, 500);
	});
	//选择支付方式
	$(document).on("click", "[f='but_msgchat_sel_pay']", function(){
		$("[f='but_msgchat_sel_pay']").removeClass("current");
		$(this).addClass("current");
		$_sdkid = $(this).attr("data-sdkid");
		$_sdkname = $(this).attr("data-sdkname");
		
		$("[f='but_msgchat_confirm_pay']").attr("data-sdkid", $_sdkid);
		$("[f='but_msgchat_confirm_pay']").attr("data-sdkname", $_sdkname);
	});
	//确定支付解锁
	$(document).on("click", "[f='but_msgchat_confirm_pay']", function(){
	//$("[f='but_msgchat_confirm_pay']").on("click", function(){
		$_sdkid = $(this).attr("data-sdkid");
		$_sdkname = $(this).attr("data-sdkname");
		if (typeof($_sdkid) == "undefined") {
			$_sdkid = "";
		}
		if (typeof($_sdkname) == "undefined") {
			$_sdkname = "";
		}
		
		$_sid = $("#msgchat_sid").val();

		if ($_sid.length == 0) {
			ToastShow("通信SID错误");
			return false;
		}
		if ($_sdkid.length == 0) {
			ToastShow("请选择支付方式");
			return false;
		}
		$.ajax({
			type: "POST",
			url: _ROOT_PATH + "index.php?m=user&c=msgchat",
			cache: false,
			data: {a:"freesid", sid:$_sid, sdkid:$_sdkid, sdkname:$_sdkname, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == "1") {
					if ($_sdkid == "999") { //余额支付
						_closeShadeBox("pop_pay_msgchat", function(){
							$("#free_msgchat_box").hide();
							$("#free_msgchat_data").hide();
							//重新加载数据
							_getChatingUserData("userright");
							_getMsgchatPageList(1);
						});
					}
					else {
						window.location.href = _ROOT_PATH+"index.php?c=pay&a=submit&paynum="+$result;
					}
				}
				else {
					$_error = $result;
					if ($_error.length == 0) {
						$_error = "解锁失败，请检查";
					}
					ToastShow($_error);
				}
			},
			error: function() {
				ToastShow("系统繁忙，请稍后再试");
			}
		});

	});
	

	//显示隐藏赠送礼物操作 7.03.25
	$(document).on("click", "[f='but_msgchat_show_gift']", function(){
		if ($("#msgchat_show_gift_box").css("display") == "none") {
			$("#msgchat_show_gift_box").show();
		}
		else {
			$("#msgchat_show_gift_box").hide();
		}
	});

	//使用模板 7.03.25
	$(document).on("click", "[f='but_open_msgchat_mb']", function(){
		if ($("#msgchat_mb_box").css("display") == "none") {
			$("#msgchat_mb_box").slideDown(100);
			$("[f='but_open_msgchat_mb']").addClass("current");
		}
		else {
			$("#msgchat_mb_box").slideUp(100);
			$("[f='but_open_msgchat_mb']").removeClass("current");
		}
	});
	$(document).on("click", "[f='but_select_msgchat_mb']", function(){
		$text = $(this).find("em").html();
		if ($text.length > 0) {
			$("#msgchat_content").val("");
			$("#msgchat_content").val($text);
			$("#msgchat_mb_box").slideUp(100);
			$("[f='but_open_msgchat_mb']").removeClass("current");
		}
	});

});

//滚动条
function _hashFooter() {
	$(".bd").scrollTop($(".msg_list").height());
}

/**
 * ajax获取我的联系人列表
 * @param:: int $page 当前页
 * @param:: int $usertype 类型 1好友0联系人
 * @return:: 
*/
function _getMsgcontactPageList($page, $usertype) {
	if (typeof($page) == "undefined") {
		$page = 1;
	}
	if (typeof($usertype) == "undefined") {
		$usertype = 0;
	}
	$obj_box = $("#chating_contact_list_data");
	$.ajax({
		type: "GET",
		url:  _ROOT_PATH+"index.php?m=user&c=msgcontact&datatype=json",
		cache: false,
		data: {page:$page, usertype:$usertype, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$prepage = $json.prepage; //上一页
			$nextpage = $json.nextpage; //下一页
			$result = $json.result;
			if ($result.length > 0) {
				if ($page == "1") {
					$obj_box.html($result);
				}
				else {
					$obj_box.append($result);
				}
				if ($nextpage > 0) { //还有下一页
					$("[f='but_get_msgcontactpage']").attr("data-page", $nextpage);
					$("[f='but_get_msgcontactpage']").show();
					$("[f='but_get_msgcontactpage']").find("label").hide();
				}
				else { 
					$("[f='but_get_msgcontactpage']").hide();
				}
			}
		},
		error: function() {
		
		}
	});
}


//AJAX请求聊天记录
function _getMsgchatPageList($page) {
	if (typeof($page) == "undefined") {
		$page = 1;
	}
	$sid = $("#msgchat_sid").val(); //SID
	$obj_box = $("#chat_log_data");
	$.ajax({
		type: "GET",
		url:  _ROOT_PATH+"index.php?m=user&c=msgchat&a=list&datatype=json",
		cache: false,
		data: {sid:$sid, page:$page, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$prepage = $json.prepage; //上一页
			$nextpage = $json.nextpage; //下一页
			$result = $json.result;
			if ($result.length > 0) {
				if ($page == "1") {
					$obj_box.html($result);
				}
				else {
					$obj_box.prepend($result);
				}
				if ($nextpage > 0) { //还有下一页
					$("[f='but_get_msgchatpage']").attr("data-page", $nextpage);
					$("[f='but_get_msgchatpage']").show();
					$("[f='but_get_msgchatpage']").find("label").hide();
				}
				else { 
					$("[f='but_get_msgchatpage']").hide();
				}
				
				if ($page == "1") {
					$(".content_box").scrollTop($("#chat_content").height());
				}
			}
		},
		error: function() {
		
		}
	});
}

//获取当前聊天会员
function _getChatingUserData($tpltype) {
	//$chat_uid= $("#msgchat_uid").val();
	$sid = $("#msgchat_sid").val();
	if ($sid.length > 0) {
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php?m=user&c=msgchat&a=chatuser",
			cache: false,
			data: {c:"msgchat", a:"chatuser", sid:$sid, tpltype:$tpltype, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				if ($response == '1') {
					if ($tpltype == 'userleft') {
						$("#chating_user_data").html($result);
						$("#chating_user_data").show();
						$("#chating_contact_list").hide();
					}
					else {
						$("#chat_log_chatuser_data").html($result);
						$("#chating_contact_list").show();
					}
				}
				else {
					if ($result.length > 0) {
						ToastShow($result);
					}
					else {
						ToastShow("获取资料失败");
					}
				}
			},
			error: function() {
			}
		});
	}
}
